<template>
  <view>
    <ConversationList :List="conversationList"></ConversationList>
  </view>
</template>

<script lang="ts" setup>
import { onShow, onLoad } from '@dcloudio/uni-app'
import { queryAccount } from '../../api/index'
import { ref, computed } from 'vue'
import store from '@/store/index'
import ConversationList from './conversation-list/index.vue'

const conversationList = computed(() => {
  return store.state.im.sessionList
})

onLoad(() => {
  // store.dispatch('im/IM_CONNECT', {
  //   accid: 'mlsk_patient_28539200',
  //   token: 'cf623fb045634dad9fe4ee3b1c2adffa',
  //   appId: '4a45279951049d52402b0396e0720dea'
  // })
  // return

  const userInfo = uni.getStorageSync('userInfo')
    ? JSON.parse(uni.getStorageSync('userInfo')).patientInfo
    : {}
  queryAccount(store.state.userInfo.patientInfo.empi)
    .then((res) => {
      if (res.data.errorCode == 'SUCCESS') {
        const { accid, token, appId } = res.data
        store
          .dispatch('im/IM_CONNECT', {
            accid: accid,
            token: token,
            appId: appId
          })
          .then(() => {
            console.log('---------------nim--------------', uni.$nim)

            // uni.$nim.V2NIMConversationService.getConversationList(0, 100).then((list) => {
            //   console.log('---------------conversationList--------------', list)
            //   conversationList.value = list.conversationList
            // })

            // uni.$nim.V2NIMConversationService.on('onConversationChanged', function (list) {
            //   console.log('---------------conversationList--------------', list)
            //   conversationList.value = list
            // })
          })
      }
    })
    .catch((e) => {
      console.log(e)
    })
})

// onShow(() => {
//   if (uni.$nim) {
//     IMListeners()
//   }
// })

// const IMListeners = () => {
//   uni.$nim.V2NIMConversationService.on('onConversationChanged', function (conversationList) {
//     console.log(conversationList)
//   })
// }
</script>

<style lang="scss" scoped>
@import '../styles/common.scss';
.bg {
  position: fixed;
  z-index: 1;
  background-color: #f3f7f8;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
page {
  height: 100vh;
  overflow: hidden;
}

.empty {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 142rpx;
  height: 100vh;
  background: #fff;
  .img {
    width: 356rpx;
    height: 276rpx;
  }
  .text {
    font-family:
      PingFang SC,
      PingFang SC;
    font-weight: 500;
    font-size: 32rpx;
    color: #222222;
    line-height: 58rpx;
    text-align: center;
    font-style: normal;
    text-transform: none;
    margin-top: 32rpx;
  }
  .btn {
    width: 558rpx;
    height: 88rpx;
    background: #00d1b6;
    border-radius: 44rpx 44rpx 44rpx 44rpx;
    font-family:
      PingFang SC,
      PingFang SC;
    font-weight: 600;
    font-size: 32rpx;
    color: #ffffff;
    line-height: 88rpx;
    text-align: center;
    margin-top: 48rpx;
  }
}
.no_data {
  overflow: hidden;
  text-align: center;
  height: 100vh;
  background: #fff;
  .img_nodata {
    margin-top: 142rpx;
    margin-bottom: 32rpx;
    width: 336rpx;
    height: 276rpx;
  }
  .img_nodata2 {
    margin-top: 202rpx;
    margin-bottom: 70rpx;
    width: 336rpx;
    height: 196rpx;
  }
  .text {
    margin: 0 auto;
    width: 488rpx;
    font-weight: 500;
    font-size: 32rpx;
    color: #222222;
    line-height: 58rpx;
  }
  .green {
    color: #00d1b6;
  }
  .button {
    margin: 48rpx auto;
    width: 558rpx;
    height: 88rpx;
    line-height: 88rpx;
    text-align: center;
    background: #00d1b6;
    border-radius: 44rpx;
    font-weight: 600;
    font-size: 32rpx;
    color: #ffffff;
  }
}
</style>
